package com.dubbo.provider.dao;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dubbo.provider.bean.Org;
import com.dubbo.provider.common.IdCreator;
import com.dubbo.provider.mapper.OrgMapper;
import com.dubbo.remote.enums.OrgTypeEnum;
import com.dubbo.remote.request.OrgCreateReq;
import com.dubbo.remote.request.OrgModifyReq;
import com.dubbo.remote.response.OrgCreateResp;

@Repository
public class OrgDao {

	@Autowired
	private OrgMapper mapper;
	@Autowired
	private IdCreator idCreator;

	public List<Org> queryList(String kinCode) {
		return mapper.selectList(buildExample(kinCode));
	}

	public List<Org> queryList(String kinCode, Integer begin, Integer pageSize) {
		return mapper.selectPage(new RowBounds(begin, pageSize), buildExample(kinCode));
	}

	public int count(String kinCode) {
		return mapper.selectCount(buildExample(kinCode));
	}

	private Wrapper<Org> buildExample(String kinCode) {
		return new EntityWrapper<Org>().eq("org_category", kinCode);
	}

	public OrgCreateResp createOrg(OrgCreateReq req) {
		Org parentOrg = getOrgByOrgCode(req.getParentOrgCode());
		Assert.notNull(parentOrg,"父机构不存在");
		Assert.isTrue(OrgTypeEnum.ORG.name().equals(parentOrg.getOrgCategory()), "上级只能是机构");
		Org merchantOrg = getOrgByOrgCode(req.getMerchantCode());
		Assert.isNull(merchantOrg, "商户编码已存在，请更换后重试");
		Org record = new Org();
		BeanUtils.copyProperties(req,record);
		record.setOrgStatus((byte) 1);
		record.setOrgLevel((byte)(parentOrg.getOrgLevel()+1));
		record.setOrgCode(idCreator.nextId());
		boolean isSuccess = mapper.insert(record) > 0;
		Assert.isTrue(isSuccess, "操作失败");
		return OrgCreateResp.builder().id(record.getId()).orgCode(record.getOrgCode()).build();
	}

	private Org getOrgByOrgCode(String parentOrgCode) {
		List<Org> collection = mapper.selectList(new EntityWrapper<Org>().eq("org_code", parentOrgCode));
		if(CollectionUtils.isEmpty(collection)) {
			return null;
		}
		return collection.get(0);
	}

	public Boolean modifyOrg(OrgModifyReq req) {
		Org org = checkExitsAndReturn(req.getId());
		org.setId(req.getId());
		org.setMobile(req.getMobile());;
		org.setOrgName(req.getOrgName());
		boolean isSuccess = mapper.updateById(org) > 0;
		Assert.isTrue(isSuccess, "操作失败");
		return true;
	}

	private Org checkExitsAndReturn(Long id) {
		Org org = mapper.selectById(id);
		Assert.notNull(org, "记录不存在");
		return org;
	}

	public Boolean enableOrg(Long id) {
		return update(id,(byte) 1);
	}

	public Boolean disableOrg(Long id) {
		return update(id,(byte) 0);
	}

	private Boolean update(Long id,byte orgStatus) {
		checkExitsAndReturn(id);
		Org organization = new Org();
		organization.setId(id);
		organization.setOrgStatus(orgStatus);
		boolean isSuccess = mapper.updateById(organization) > 0;
		Assert.isTrue(isSuccess, "操作失败");
		return Boolean.TRUE;
	}

}
